﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using INTEX.DataBase.Tools;
using System.Data.SQLite;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class MVTEXAPP : DocumentBase
    {
        #region-------------Properties-----------------------
        string dbConnection = "C:/IntexReport/Data/MVTEXAPP/APP.s3db";
        string sql = "";
        bool ModeEdit = false;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        string ROWNID = "";
        bool IsLoaded = false;
        DataRow CurrentRow = null;
        DataSet SResult = null;
        DataRow LastRow = null;
        DataSet SetMap = null;
        public DataSet dsResult;
        MVTEXAPPData m_Data;
        MVTEXAPPReport m_rpt;
        DataTable tbUser = null;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }

        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        CollectionViewSource vwTVKP
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTVKP");
            }
        }
        DataRow dwTVKP
        {
            get
            {
                if (vwTVKP.View == null)
                    return null;
                if ((DataRowView)vwTVKP.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTVKP.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwHVKP
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwHVKP");
            }
        }
        DataRow dwHVKP
        {
            get
            {
                if (vwHVKP.View == null)
                    return null;
                if ((DataRowView)vwHVKP.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwHVKP.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWEFT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWEFT");
            }
        }
        DataRow dwWEFT
        {
            get
            {
                if (vwWEFT.View == null)
                    return null;
                if ((DataRowView)vwWEFT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWEFT.View.CurrentItem).Row;
            }
        }
        string ART
        {
            get
            {
                return m_Data.Left(txtART.Text.ToString() + "                      ", 20);
            }
        }
        long NOrder
        {
            get
            {
                return Convert.ToInt64(txtNOrder.Text.ToString());
            }
            set
            {
                txtNOrder.Text = value.ToString();
            }
        }
        string currentorder = "0";
        double USD2RMB
        {
            get
            {
                return Convert.ToDouble(txtR1.Text.ToString());
            }
            set
            {
                txtR1.Text = value.ToString();
            }
        }
        double RATE1
        {
            get
            {
                return Convert.ToDouble(txtR2.Text.ToString());
            }
            set
            {
                txtR2.Text = value.ToString();
            }
        }

        string NART
        {
            get
            {
                return m_Data.Left(txtNArt.Text.ToString().PadRight(24), 24);
            }
            set
            {
                txtNArt.Text = value;
            }
        }
        string NColor
        {
            get
            {
                return m_Data.Left(txtNFColor.Text.ToString().PadRight(10), 10);
            }
            set
            {
                txtNFColor.Text = value;
            }
        }
        BindingListCollectionView cvLArt
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgART.ItemsSource);
            }
        }
        BindingListCollectionView cvNOrder
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgNOrders.ItemsSource);
            }
        }
        BindingListCollectionView cvVKP
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgVKP.ItemsSource);
            }
        }

        int pos = 1;
        string user = "";
        SQLiteDatabase SQLiteDatabase;
        DataTable TBResult;
        #endregion
        public MVTEXAPP()
        {
            InitializeComponent();
            this.Name = "展会程序";
            DataContext = this;
            m_Data = new MVTEXAPPData();
            SQLiteDatabase = new SQLiteDatabase(dbConnection);
            if (!SQLiteDatabase.CheckTable("ICC_EXART"))
            {
                SQLiteDatabase.CreateICC_EXART();
            }
            if (!SQLiteDatabase.CheckTable("ICC_EXORD"))
            {
                SQLiteDatabase.CreateICC_EXORD();
            }
            if (!SQLiteDatabase.CheckTable("ICC_MAXORD"))
            {
                SQLiteDatabase.CreateICC_MAXORD();
            }


        }
        ~MVTEXAPP()
        {

        }
        private void DocumentBase_Closed(object sender, EventArgs e)
        {

            try
            {
                m_Data.Save(SetData);
                m_Data.SaveExcel(SetData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        /// <summary>
        /// SetProd 当前订单
        /// SetData保存所有订单，但是用于展示今日订单
        /// SetEnv保存所有订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                EditCurrent.Visibility = Visibility.Visible;
                ModeEdit = false;
                vwHVKP.View.CurrentChanged += View_CurrentChanged;

                //cvVKP.SortDescriptions.Add(new System.ComponentModel.SortDescription("VKPAUFPOS", ListSortDirection.Descending));
                //  Load_Click(null, null); 
                //cmbNDate.Text = m_Data.date; 
                /*sql = "SELECT* FROM ICC_EXART";
                TBResult = SQLiteDatabase.GetDataTable(sql);
                SetEnv.ICC_EXART.Clear();
                SetEnv.Merge(TBResult);*/
                sql = "SELECT* FROM ICC_MAXORD";
                TBResult = SQLiteDatabase.GetDataTable(sql);
                if (SetEnv.Tables.Contains("ICC_MAXORD"))
                {
                    SetEnv.Tables["ICC_MAXORD"].Clear();
                }
                SetEnv.Merge(TBResult);
                foreach (DataRow row in SetEnv.Tables["ICC_MAXORD"].Rows)
                {
                    NOrder = Convert.ToInt64(row["MAXORD"].ToString());
                    USD2RMB = Convert.ToDouble(row["USD2RMB"].ToString());
                    RATE1 = Convert.ToDouble(row["RATE1"].ToString());
                }
                //Load_Click(null, null); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        void View_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                if (dwHVKP == null)
                    return;
                string filter = "VKPAUFKRS='" + dwHVKP["VKPAUFKRS"] + "' AND VKPAUFLFD='" + dwHVKP["VKPAUFLFD"].ToString() + "'";
                DataRow[] rows = SetData.ICC_EXORD.Select(filter);
                DataTable tb = rows.CopyToDataTable();
                tb.TableName = "ICC_EXORD";
                SetProd.Clear();
                SetProd.Merge(tb);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                Save_Click(null, null);
                m_rpt = new MVTEXAPPReport();
                m_rpt.dsSource = SetProd;
                m_rpt.WriteExcelReport("OpenExcel");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if(ModeEdit)
                {
                    ModeEdit=false;
                    EditCurrent.Visibility=Visibility.Collapsed;
                }
                string MAXORD = "1";
                string _LFD = "";

                if (SetEnv.Tables.Contains("ICC_MAXORD"))
                {
                    foreach (DataRow row in SetEnv.Tables["ICC_MAXORD"].Rows)
                    {
                        row["MAXORD"] = NOrder;
                    }
                }
                if (SetProd.ICC_EXORD.Rows.Count == 0)
                    return;
                vwHVKP.View.MoveCurrentTo(-1);
                vwTVKP.View.MoveCurrentTo(-1);

                sql = "UPDATE ICC_MAXORD SET MAXORD= '" + NOrder + "',USD2RMB=" + USD2RMB + ", RATE1=" + RATE1; ;
                SQLiteDatabase.ExecuteNonQuery(sql);
                foreach (DataRow row in SetProd.ICC_EXORD.Rows)
                {
                    _LFD = row["VKPAUFLFD"].ToString().Trim();
                    break;
                }


                SQLiteDatabase.ExecuteNonQuery("DELETE FROM ICC_EXORD WHERE RTRIM(VKPAUFLFD)='" + _LFD + "' ");

                string filter = "VKPART like 'PleaseEnter%' OR ITEM like '0%' ";

                DataRow[] rows = SetProd.ICC_EXORD.Select(filter);
                foreach (DataRow row in rows)
                {
                    row.Delete();
                }
                SetProd.AcceptChanges();
                sql = "insert into ICC_EXORD(ROWNID,VKPAUFKRS,VKPAUFLFD,VKPAUFPOS,ITEM,VKPART,VKPARTDES,VKPFARBE,VKPCDES,TWIDTH,VWIDTH,GSQM,GM,YARN,PRICE,SPRICE,COMPOSITION,ORDTYPE,QUANTITY,CUSTOMER,VKPNEUDAT,VKPNEUTIME,VKPNUEBEN,REMARK,NUMBER1,NUMBER2,NUMBER3,NUMBER4,NUMBER5,CHAR1,CHAR2,CHAR3,CHAR4,CHAR5) values ";
                string values = "";
                int idx = 1;
                foreach (DataRow row in SetProd.ICC_EXORD.Rows)
                {
                    string VKPART = row["VKPART"].ToString();
                    if (VKPART.StartsWith("PleaseEnter"))
                        continue;
                    _LFD = row["VKPAUFLFD"].ToString().Trim();
                    values += "(";
                    values += "'" + row["ROWNID"].ToString() + "',";
                    values += "'" + row["VKPAUFKRS"].ToString() + "',";
                    values += "'" + row["VKPAUFLFD"].ToString() + "',";
                    values += "'" + row["VKPAUFPOS"].ToString() + "',";
                    values += "'" + row["ITEM"].ToString() + "',";
                    values += "'" + row["VKPART"].ToString() + "',";
                    values += "'" + row["VKPARTDES"].ToString() + "',";
                    values += "'" + row["VKPFARBE"].ToString() + "',";
                    values += "'" + row["VKPCDES"].ToString() + "',";
                    values += "'" + row["TWIDTH"].ToString() + "',";
                    values += "'" + row["VWIDTH"].ToString() + "',";
                    values += "'" + row["GSQM"].ToString() + "',";
                    values += "'" + row["GM"].ToString() + "',";
                    values += "'" + row["YARN"].ToString() + "',";
                    values += "'" + row["PRICE"].ToString() + "',";
                    values += "'" + row["SPRICE"].ToString() + "',";
                    values += "'" + row["COMPOSITION"].ToString() + "',";
                    values += "'" + row["ORDTYPE"].ToString() + "',";
                    values += "'" + row["QUANTITY"].ToString() + "',";
                    values += "'" + row["CUSTOMER"].ToString() + "',";
                    values += "'" + row["VKPNEUDAT"].ToString() + "',";
                    values += "'" + row["VKPNEUTIME"].ToString() + "',";
                    values += "'" + row["VKPNUEBEN"].ToString() + "',";
                    values += "'" + row["REMARK"].ToString() + "',";
                    values += "'" + row["NUMBER1"].ToString() + "',";
                    values += "'" + row["NUMBER2"].ToString() + "',";
                    values += "'" + row["NUMBER3"].ToString() + "',";
                    values += "'" + row["NUMBER4"].ToString() + "',";
                    values += "'" + row["NUMBER5"].ToString() + "',";
                    values += "'" + row["CHAR1"].ToString() + "',";
                    values += "'" + row["CHAR2"].ToString() + "',";
                    values += "'" + row["CHAR3"].ToString() + "',";
                    values += "'" + row["CHAR4"].ToString() + "',";
                    values += "'" + row["CHAR5"].ToString() + "'),";
                    idx++;
                }

                values += "***";
                values = values.Replace(",***", ";");
                sql += values;
                SQLiteDatabase.ExecuteNonQuery(sql);
                sql = "DELETE FROM ICC_EXORD WHERE VKPART like 'PleaseEnter%' ";
                SQLiteDatabase.ExecuteNonQuery(sql);
                filter = "TRIM(VKPAUFLFD)='" + _LFD + "' ";
                rows = SetData.ICC_EXORD.Select(filter);
                foreach (DataRow row in rows)
                {
                    row.Delete();
                }
                foreach (DataTable tb in SetProd.Tables)
                {
                    SetData.Merge(tb, false, MissingSchemaAction.Ignore);
                }
                //cvVKP.SortDescriptions.Add(new System.ComponentModel.SortDescription("VKPAUFPOS", ListSortDirection.Descending));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void NewVKP(DataRow dwInput)
        {
            try
            {
                DataRow dwData = SetProd.ICC_EXORD.Rows.Add();
                if (dwInput != null)
                {
                    dwData.ItemArray = dwInput.ItemArray;
                }
                else
                {
                    dwData["NUMBER1"] = USD2RMB;
                    dwData["NUMBER2"] = RATE1;
                    dwData["NUMBER3"] = "0";
                    dwData["NUMBER4"] = "0";
                    dwData["NUMBER5"] = "0";
                    dwData["CHAR1"] = "   ";
                    dwData["CHAR2"] = "   ";
                    dwData["CHAR3"] = "   ";
                    dwData["CHAR4"] = "   ";
                    dwData["CHAR5"] = "   ";
                }
                dwData["VKPAUFKRS"] = "EX";
                dwData["VKPAUFLFD"] = currentorder;
                dwData["VKPAUFPOS"] = Helpers.Helper.Right("000" + (pos++).ToString(), 3);
                dwData["VKPNEUDAT"] = m_Data.date;
                dwData["ITEM"] = " ";
                dwData["VKPART"] = "PleaseEnter";
                dwData["CUSTOMER"] = Helpers.Helper.Left(txtNKD.Text.ToString().PadRight(40), 40);
                dwData["QUANTITY"] = 1;
                string time= System.DateTime.Now.ToLocalTime().ToString();
                //MessageBox.Show(time);
                dwData["VKPNEUTIME"] = time;
                dwData["VKPNUEBEN"] = txtUser.Text;
                dwData["ROWNID"] = dwData["VKPAUFLFD"].ToString() + "." + Helpers.Helper.Right("000" + dwData["VKPAUFPOS"].ToString(), 3);
                dwData["ITEM"] = "0";
                dwData["VKPARTDES"] = "0";
                dwData["VKPFARBE"] = "0";
                dwData["VKPCDES"] = "0";
                dwData["TWIDTH"] = "0";
                dwData["VWIDTH"] = "0";
                dwData["GSQM"] = "0";
                dwData["GM"] = "0";
                dwData["YARN"] = "0";
                dwData["PRICE"] = "0";
                dwData["SPRICE"] = "0";
                dwData["COMPOSITION"] = "0";
                dwData["ORDTYPE"] = "0";
                dwData["QUANTITY"] = "0";
                dwData["VKPNUEBEN"] = "0";
                dwData["REMARK"] = "0";

                if (vwTVKP.View != null)
                {
                    vwTVKP.View.MoveCurrentToFirst();
                }
                NART = "";
                NColor = "";
                txtNArt.Focus();
                cvVKP.SortDescriptions.Add(new System.ComponentModel.SortDescription("ROWNID", ListSortDirection.Descending));
                if (cvVKP.SortDescriptions.Count > 2)
                {
                    cvVKP.SortDescriptions.RemoveAt(0);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        private void btnAddVKP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtNKD.Text.Trim() == "")
                {
                    MessageBox.Show("Please mark your customer!");
                    return;
                }
                //txtNArt.Text = "J4-0599"; 

                if (!ModeEdit)
                {
                    Save_Click(null, null);
                    NOrder++;
                    currentorder = NOrder.ToString();
                    pos = 1;
                    SetProd.Clear();
                }
                EditCurrent.Visibility = Visibility.Collapsed;
                ModeEdit = false;
                NewVKP(null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        private void btnConfirmLine_Click(object sender, RoutedEventArgs e)
        {
            if (dwTVKP == null)
            {
                MessageBox.Show("Please Locate a record");
            }
            try
            {
                txtNArt.Focus();
                NewVKP(dwTVKP);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        private void Load_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                //EditCurrent.Visibility = Visibility.Visible;
                //ModeEdit = true;
                sql = "SELECT* FROM ICC_EXORD";
                TBResult = SQLiteDatabase.GetDataTable(sql);
                SetData.Clear();
                try
                {
                    SetData.Merge(TBResult);
                }
                catch (Exception exx)
                {
                    try
                    {
                        SetData.Merge(TBResult);
                    }
                    catch (Exception xxx)
                    { }
                }
                cmbNDate.Items.Clear();
                var query = from date in SetData.ICC_EXORD.AsEnumerable()
                            group date by date.Field<string>("VKPNEUDAT") into g
                            select new
                            {
                                KEY = g.Key
                            };
                foreach (var date in query)
                {
                    ComboBoxItem cb = new ComboBoxItem();
                    cmbNDate.Items.Add(cb);
                    cb.Content = date.KEY.ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Conver2xml_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_Data.Convert2xml();
                Load_Click(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        private void Convert2exl_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_Data.SaveExcel(SetData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void txtNArt_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key != System.Windows.Input.Key.Enter)
                    return;
                DataRow vart = null;
                dwTVKP["VKPART"] = NART.ToUpper();
                dwTVKP["VKPFARBE"] = NColor;
                dwTVKP["ITEM"] = NART.Trim().ToUpper() + "-" + NColor.Trim();
                sql = "SELECT* FROM ICC_EXART WHERE RTRIM(ARTICLE) = '" + NART.Trim().ToUpper() + "'";
                TBResult = SQLiteDatabase.GetDataTable(sql);
                /*
                var qart = from DataRow row in SetEnv.ICC_EXART.AsEnumerable()
                           where row["ARTICLE"].ToString().Trim() == NART.Trim().ToUpper()
                           select row;*/
                foreach (DataRow art in TBResult.Rows)
                {
                    vart = art;
                }
                if (vart != null)
                {
                    dwTVKP["VKPARTDES"] = vart["DESCRIPTION"];
                    dwTVKP["TWIDTH"] = vart["TWIDTH"];
                    dwTVKP["VWIDTH"] = vart["VWIDTH"];
                    dwTVKP["GSQM"] = vart["GSQM"];
                    dwTVKP["GM"] = vart["GM"];
                    dwTVKP["YARN"] = vart["YARN"];
                    double price = Convert.ToDouble(vart["PRICE"].ToString());

                    dwTVKP["PRICE"] = vart["PRICE"];
                    dwTVKP["NUMBER1"] = USD2RMB;
                    dwTVKP["NUMBER2"] = RATE1;
                    double spr = price * USD2RMB + RATE1;
                    double sprice = 0.0,sprice1=0.0;
                    sprice1 = Math.Floor(spr*100)*0.01;
                    sprice = Math.Ceiling(sprice1 * 10) * 0.1;
                    if (price == 0)
                        sprice = 0;
                    dwTVKP["SPRICE"] = sprice;
                    dwTVKP["COMPOSITION"] = vart["COMPOSITION"];
                }

                dwTVKP["VKPFARBE"] = "";
                dwTVKP["REMARK"] = "";
                if (ModeEdit)
                    return;
                btnConfirmLine_Click(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void cmdFilterART_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string filter = "ARTICLE LIKE '%" + txtLART.Text.ToString().Trim() + "%'";
                cvLArt.CustomFilter = filter;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void btnArtClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                cvLArt.CustomFilter = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }
        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string filter = "VKPAUFLFD LIKE '" + txtLFD.Text.ToString().Trim() + "%' AND VKPART LIKE '" + ART.Trim() + "%' AND VKPNEUDAT LIKE '" + cmbNDate.Text.ToString().Trim() + "%'";
                cvNOrder.CustomFilter = filter;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }

        }
        private void btnFilterClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                cvNOrder.CustomFilter = null;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void btnResetDB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (INTEXDBC == null)
                {
                    app.Load();
                }
                DataSet SResult = INTEXDBC.QueryVAART(null, 4);
                if (SResult.Tables.Count > 0)
                {
                    SResult.Tables[0].TableName = "ICC_EXART";

                    Convert2LocalDB(SResult.Tables[0]);
                    SetEnv.ICC_EXART.Clear();
                    SetEnv.Merge(SResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Convert2LocalDB(DataTable tbArt)
        {
            try
            {
                string sql = "DELETE FROM ICC_EXART";
                SQLiteDatabase.ExecuteNonQuery(sql);
                //sql = "DELETE FROM ICC_EXORD";
                //SQLiteDatabase.ExecuteNonQuery(sql);
                sql = "insert into ICC_EXART(ARTICLE,DESCRIPTION,TWIDTH,VWIDTH,GSQM,GM,YARN,PRICE,SPRICE,COMPOSITION,REMARK) values ";
                string values = "";
                int idx = 1;
                int ids = -1;
                int smallidx = 0;
                foreach (DataRow row in tbArt.Rows)
                {
                    values += "(";
                    values += "'" + row["ARTICLE"].ToString() + "',";
                    values += "'" + row["DESCRIPTION"].ToString().Replace("'", "^") + "',";
                    values += "'" + row["TWIDTH"].ToString() + "',";
                    values += "'" + row["VWIDTH"].ToString() + "',";
                    values += "'" + row["GSQM"].ToString() + "',";
                    values += "'" + row["GM"].ToString() + "',";
                    values += "'" + row["YARN"].ToString().Replace("'", "^") + "',";
                    string PRICE = row["PRICE"].ToString().Replace("RMB", "");
                    ids = PRICE.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
                    if (ids > 0)
                    {
                        PRICE = PRICE.Substring(ids);
                    }

                    ids = PRICE.IndexOfAny(new char[] { '/' });
                    if (ids > 0)
                    {
                        PRICE = Helpers.Helper.Left(PRICE, ids);
                    }
                    if (PRICE.Trim() == "")
                        PRICE = "0";
                    values += "'" + PRICE + "',";
                    values += "'" + PRICE + "',";
                    values += "'" + row["COMPOSITION"].ToString().ToString().Replace("'", "^") + "',";
                    values += "'" + Helpers.Helper.GetGB2312Value(row["WALOHNA"].ToString().Replace("'", "^")) + "'),";
                    idx++;
                    smallidx++;
                    if (smallidx > 100)
                    {
                        smallidx = 0;
                        values += "***";
                        values = values.Replace(",***", ";");
                        sql += values;
                        SQLiteDatabase.ExecuteNonQuery(sql);
                        sql = "insert into ICC_EXART(ARTICLE,DESCRIPTION,TWIDTH,VWIDTH,GSQM,GM,YARN,PRICE,SPRICE,COMPOSITION,REMARK) values ";
                        values = "";
                    }
                }
                values += "***";
                values = values.Replace(",***", ";");
                sql += values;
                SQLiteDatabase.ExecuteNonQuery(sql);
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ModeEdit = true;
                EditCurrent.Visibility = Visibility.Visible;
                tabEdit.Focus();

                foreach (DataRow row in SetProd.ICC_EXORD.Rows)
                {
                    currentorder = row["VKPAUFLFD"].ToString();
                    txtNKD.Text = row["CUSTOMER"].ToString();
                    int current = Convert.ToInt16(row["VKPAUFPOS"].ToString());
                    if (current > pos)
                        pos = current;
                }
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Del_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dwTVKP["VKPART"] = "PleaseEnter";
                return;
                string filter = "ROWNID='" + dwTVKP["ROWNID"].ToString() + "'";
                DataRow[] rows = SetProd.ICC_EXORD.Select(filter);

                foreach (DataRow row in rows)
                {
                    row["VKPART"] = "PleaseEnter";
                    //row.Delete();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void btnLoadArt_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtLART.Text.ToString().Trim().Length < 3)
                {
                    //MessageBox.Show("At least first 3 charactor");
                    //return;
                }
                sql = "SELECT DISTINCT M.* FROM ICC_EXART M WHERE  ARTICLE LIKE '" + txtLART.Text.ToString().Trim() + "%'";
                TBResult = SQLiteDatabase.GetDataTable(sql);
                SetEnv.ICC_EXART.Clear();
                SetEnv.Merge(TBResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                try
                {
                    SetEnv.ICC_EXART.Clear();
                    SetEnv.Merge(TBResult);
                }
                catch (Exception exx)
                { }
            }
        }
    }
}
